package com.he.hsdyc.common.log;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class GlobalInterceptor implements HandlerInterceptor {


//	private static final Logger log = Logger.getLogger(GlobalInterceptor.class);

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
	     //log.info("-GlobalInterceptor请求结束-");
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		// log.info("-MyInterceptor请求处理之后调用-");
		//log.info("RESPONSE:"+JSON.toJSONString(arg2));
		log.info("-------处理完成END------" );
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws IOException {
		try {
			HandlerMethod method = (HandlerMethod) obj;
            /*MethodParameter[] methodParameters = method.getMethodParameters();
            for(MethodParameter methodParameter : methodParameters){
                System.out.println(methodParameter.getParameterName());
            }*/
			log.info("-------处理开始START------");
			log.info("请求Controller: < " + method.getBeanType().getName() + " >");
			log.info("请求方法: < " + method.getMethod().getName() + " > ");
			log.info("请求参数："+ JSONObject.toJSONString(request.getParameterMap()));
			log.info("请求方式："+ request.getMethod());
			log.info("请求路径："+request.getRequestURL());
			return true;
		} catch (Exception e) {
			return true;
		}
	}

}
